%read in the data from databuild_contractindex.do or
%databuild_closingdated.do or databuild_listprice.do depending on the type
%of index desired--just need to modify file names.
%the code is organized similarly to estimation_list.m, so there are some
%steps that are not necessary for computing a simple contract dated index.
%See also estimation_list.m and estimation_sales.m for helpful notes

data=importdata('dc_salepr_close_mls.txt');
datat=data;
data=importdata('la_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('pho_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('seattle_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('sanfran_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('chi_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('denver_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('sandiego_salepr_close_mls.txt');
datat=[datat;data];
data=importdata('vegas_salepr_close_mls.txt');
datat=[datat;data];
data=datat;

adj=0;S=1;
N=length(data);weights=data(1:52,15:end);
rw3=data(:,9);rw3=(adj==0)+(adj==1).*rw3;
myo=data(:,2);my0o=data(:,3);salepro=data(:,4);rwo=data(:,7);rw2o=data(:,8);rw3o=data(:,9);rw3o=(adj==0)+(adj==1).*rw3o;delta0o=data(:,14);
ido=data(:,10);seas=data(:,11);houseido=data(:,12); weeknumo=data(:,13);

%number of delistings each week
weeknumot=unique(weeknumo);
for i=1:length(weeknumot)
delist(i,1)=sum((weeknumo==weeknumot(i)).*rw3o);
end
%create unique weeknum identifier
for i=1:N    
    weeknumo(i)=find(weeknumo(i)==weeknumot);
end

%5 is unadjusted price; 6 is adjusted price
priceo=data(:,5+adj);

%interval weights
rwo=rw2o.*rw3o;

datao=[weeknumo my0o salepro priceo rwo houseido myo delta0o ];
data=datao;
weeknumo=data(:,1);my0o=data(:,2);salepro=data(:,3);priceo=data(:,4);rwo=data(:,5);houseido=data(:,6);myo=data(:,7);delta0o=data(:,8); 

%make S copies of each observations
weeknumo=repmat(weeknumo,1,S);
my0o=repmat(my0o,1,S);salepro=repmat(salepro,1,S);priceo=repmat(priceo,1,S);rwo=repmat(rwo,1,S);houseido=repmat(houseido,1,S);myo=repmat(myo,1,S);delta0o=repmat(delta0o,1,S);

weeknum=reshape(weeknumo,length(weeknumo)*S,1);my0o=reshape(my0o,length(my0o)*S,1);salepro=reshape(salepro,length(salepro)*S,1);
priceo=reshape(priceo,length(priceo)*S,1);rwo=reshape(rwo,length(rwo)*S,1);delta0o=reshape(delta0o,length(rwo)*S,1);
fulldatao=[weeknum my0o salepro priceo rwo delta0o];
weeknum=fulldatao(:,1);my0=fulldatao(:,2);salepr=fulldatao(:,3);price=fulldatao(:,4);rw=fulldatao(:,5);delta0=fulldatao(:,6);

N=length(rw);


yv=rw.*log(price)-rw.*log(salepr)+rw.*delta0;
%form the X and Z matrices in case shiller; 
Z=zeros(N,length(unique(weeknum)));X=Z;
for i=1:size(Z,2)
    X(:,i)=price.*(weeknum==i);    
    Z(:,i)=rw.*(weeknum==i);    
end


A=Z'*Z;B=Z'*yv;

coeffadj=A\B;
out=[weeknumot,coeffadj(1:length(weeknumot)),delist];

%suffix smp denotes sale price index, only homes on mls
%suffied spl denotes contract dated index
%suffix sp denotes sale price index
if(adj==1)
save compweekspm.txt out -ascii -double -tabs 
else
save compweekspm.txt out -ascii -double -tabs 
end
